#!/env/bin/python
# coding:utf-8


from selenium import webdriver
from datetime import datetime,date,timedelta
import time
import xlrd
import logging

driver = webdriver.Firefox()
def return_driver():
    return driver

def open_Url(url):
    driver.get(url)
    time.sleep(3)

def id(element):
    return driver.find_element_by_id(element)

def name(element):
    return driver.find_element_by_name(element)

def css(element):
    return driver.find_element_by_css_selector(element)

def xpath(element):
    return driver.find_element_by_xpath(element)

def js(element):
    driver.execute_script("document.getElementById(" + "'"+ element +"'" + ").removeAttribute('readonly')")

def date_n(n):
    return str((date.today() + timedelta(days = + int(n))).strftime("%Y-%m-%d"))

def read_excel(filename,index):
    #指定要打开的xlsx文件
    xls = xlrd.open_workbook(filename)
    #指定要选择的sheet
    sheet = xls.sheet_by_index(index)
    print(sheet.nrows)
    print(sheet.ncols)
    dict = {}
    for i in range(sheet.nrows):
        data = []
        for j in range(sheet.ncols):
            data.append(sheet.row_values(i)[j])
        dict[i] = data
    return dict

def log(str):
    logging.basicConfig(level=logging.INFO,format='%(asctime)s %(filename)s %(levelname)s %(message)s',
                        datefmt='%a, %d %b %Y %H:%M:%S',filename="log-selenium.log",
                        filemode='a')
    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
    formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
    console.setFormatter(formatter)
    logging.getLogger("").addHandler(console)
    logging.info(str)

